package com.shangcheng.server.controller;

import com.shangcheng.model.domain.SmsFlashPromotion;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.service.SmsFlashPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("flash")
public class SmsFlashPromotionController {

    @Autowired
    private SmsFlashPromotionService smsCouponHistoryService;

    /**
     * 添加活动
     *
     * @param flashPromotion
     * @return
     */
    @PostMapping("create")
    public Result create(@RequestBody SmsFlashPromotion flashPromotion) {
        smsCouponHistoryService.create(flashPromotion);
        return new Result(200, null, "添加成功");
    }

    /**
     * 删除活动
     *
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        smsCouponHistoryService.delete(id);
        return new Result(200, null, "删除成功");
    }

    /**
     * 编辑活动
     *
     * @param id
     * @return
     */
    @PostMapping("update/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody SmsFlashPromotion flashPromotion) {
        SmsFlashPromotion promotion = smsCouponHistoryService.update(id, flashPromotion);
        return new Result(200, promotion, "修改成功");
    }

    /**
     * 修改上下线状态
     *
     * @param id
     * @return
     */
    @PostMapping("update/status/{id}")
    public Result status(@PathVariable("id") Integer id, Integer status) {
        SmsFlashPromotion couponHistory = smsCouponHistoryService.status(id, status);
        return new Result(200, couponHistory, "修改成功");
    }

    /**
     * 获取活动详情
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result findCouponHistory(@PathVariable("id") Integer id) {
        SmsFlashPromotion couponHistory = smsCouponHistoryService.findCouponHistory(id);
        return new Result(200, couponHistory, "修改成功");
    }

    /**
     * 根据活动名称分页查询
     *
     * @return
     */
    @GetMapping("list")
    public Result findAllSmsFlashPromotion(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           String keyword) {
        PageResult pr = smsCouponHistoryService.list(pageNum, pageSize, keyword);
        return new Result(200, pr, "");
    }
}
